<?xml version="1.0" encoding="windows-1250"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="xml" encoding="windows-1250" indent="yes"/> 

  <xsl:param name="concepts_interm"/>
  <xsl:param name="views_interm"/>
  <xsl:param name="package"/>

  <xsl:param name="cviews" select="document(translate($views_interm, '\', '/'))/node()"/>
  <xsl:param name="cinterm" select="document(translate($concepts_interm, '\', '/'))"/>

  <xsl:template match="@*">
    <xsl:copy/> 
  </xsl:template>
 
  <xsl:template match="/">
    <CViews>
      <xsl:apply-templates select="/Gen/node()/CViews/CView"/> 
    </CViews>
  </xsl:template>

  <xsl:template match="CView">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="package"><xsl:value-of select="$package"/></xsl:attribute>
      <xsl:copy-of select="CVTitles"/>
      <xsl:apply-templates select="CVInclude"/>
    </xsl:copy>
  </xsl:template>

 <xsl:template match="CVInclude">
   <xsl:variable name="c" select="$cinterm/Concepts/Concept[@id_concept=current()/@id_concept]"/>
   <xsl:copy>
     <xsl:apply-templates select="@*"/>     
     <xsl:attribute name="cvname"><xsl:value-of select="ancestor::CView/@name"/></xsl:attribute>     
     <xsl:attribute name="concept"><xsl:value-of select="Concept/@name"/></xsl:attribute>     
     <xsl:attribute name="conceptcoll"><xsl:value-of select="Concept/@coll"/></xsl:attribute>          
     <xsl:attribute name="id_cvincludeparent">
       <xsl:choose>
         <xsl:when test="name(..)='CView'"><xsl:value-of select="@id_cvinclude"/></xsl:when>
         <xsl:otherwise>
           <xsl:value-of select="../../../@id_cvinclude"/>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:attribute>          
     <xsl:attribute name="coverer">coverer_<xsl:call-template name="getcovroot"><xsl:with-param name="concept" select="@id_concept"/><xsl:with-param name="hint" select="@id_cvinclude"/></xsl:call-template></xsl:attribute>
     <xsl:attribute name="covrootpath"><xsl:call-template name="getcovpath"><xsl:with-param name="concept" select="@id_concept"/><xsl:with-param name="hint" select="@id_cvinclude"/></xsl:call-template></xsl:attribute>

     <xsl:variable name="nickname">
       <xsl:choose>
         <xsl:when test="@path='../..'">
           <xsl:choose>
             <xsl:when test="../../../../../../@nickname"><xsl:value-of select="../../../../../../@nickname"/></xsl:when>
             <xsl:otherwise><xsl:value-of select="../../../../../../Concept/@name"/></xsl:otherwise>
           </xsl:choose>
         </xsl:when>
         <xsl:when test="@nickname"><xsl:value-of select="@nickname"/></xsl:when>
         <xsl:otherwise><xsl:value-of select="Concept/@name"/></xsl:otherwise>
       </xsl:choose>
     </xsl:variable>
     <xsl:attribute name="nickname">
       <xsl:value-of select="$nickname"/>
     </xsl:attribute>
     <xsl:attribute name="nicknamelow">
       <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$nickname"/></xsl:with-param></xsl:call-template>
     </xsl:attribute>
     <xsl:attribute name="nickcoll">
       <xsl:choose>
         <xsl:when test="@nickcoll"><xsl:value-of select="@nickcoll"/></xsl:when>
         <xsl:otherwise><xsl:value-of select="Concept/@coll"/></xsl:otherwise>
       </xsl:choose>
     </xsl:attribute>     
     <xsl:variable name="rootpath"><xsl:for-each select="ancestor-or-self::CVInclude">/<xsl:value-of select="Concept/@name"/></xsl:for-each></xsl:variable>
     <xsl:attribute name="rootpath">
       <xsl:value-of select="$rootpath"/>
     </xsl:attribute>     
     <xsl:variable name="rootnickpath">
       <xsl:for-each select="ancestor-or-self::CVInclude">/<xsl:choose>
           <xsl:when test="@nickname"><xsl:value-of select="@nickname"/></xsl:when>
           <xsl:otherwise><xsl:value-of select="Concept/@name"/></xsl:otherwise>
         </xsl:choose>
       </xsl:for-each>
     </xsl:variable>
     <xsl:attribute name="rootnickpath">
       <xsl:value-of select="$rootnickpath"/>
     </xsl:attribute>
     <xsl:attribute name="required">
       <xsl:choose>
         <xsl:when test="../@required"><xsl:value-of select="../@required"/></xsl:when>
         <xsl:otherwise>*</xsl:otherwise>
       </xsl:choose>
     </xsl:attribute>
     <xsl:if test="$c/@xxxrecursive"><xsl:attribute name="xxxrecursive"><xsl:value-of select="$c/@xxxrecursive"/></xsl:attribute></xsl:if>
     <xsl:copy-of select="Concept"/>      
     <xsl:apply-templates select="CVIStyles"/>
     <xsl:copy-of select="CVICGroups"/>

     <xsl:if test="$cinterm/Concepts/Concept/CAssociations/CAssociation[(@type='ref' or (@type='navigable' and (@cardinality='n:1' or @cardinality='1:1'))) and @id_cchild=current()/@id_concept]">
        <ConsistencyChecks>
          <xsl:for-each select="$cinterm/Concepts/Concept/CAssociations/CAssociation[(@type='ref' or (@type='navigable' and (@cardinality='n:1' or @cardinality='1:1'))) and @id_cchild=current()/@id_concept]">
            <ConsistencyCheck concept="{$cinterm/Concepts/Concept[@id_concept=current()/@id_cparent]/@name}" conceptcoll="{$cinterm/Concepts/Concept[@id_concept=current()/@id_cparent]/@coll}" association="{@id_cparent}_{@linkname}">
              <xsl:copy-of select="CATitles"/>
              <CTitles>
                <xsl:copy-of select="../../CTitles/CTitle[@id_cttype='maintitle']"/>
              </CTitles>
              <Works>
                <xsl:variable name="c"><xsl:value-of select="@id_cparent"/></xsl:variable>
                <xsl:variable name="covrootconcept"><xsl:call-template name="getcovroot"><xsl:with-param name="concept" select="@id_cparent"/></xsl:call-template></xsl:variable>
                
                <xsl:for-each select="$cviews/CViews/CView[@cangenerate='1' and @active='1' and CVInclude/@id_concept=$cinterm/Concepts/Concept[@name=$covrootconcept]/@id_concept and descendant-or-self::CVInclude/@id_concept=$c]">
                  <Work name="{@name}">
                    <Includes>
                      <xsl:for-each select="descendant-or-self::CVInclude[@id_concept=$c and (not(@path) or @path='')]">
                        <Include id_cvinclude="{@id_cvinclude}">
                          <xsl:attribute name="path">
                            <xsl:for-each select="ancestor-or-self::CVInclude">
                              <xsl:sort select="count(ancestor-or-self::CVInclude)"/>
                              <!--<xsl:value-of select="concat('/',$cinterm/Concepts/Concept[@id_concept=current()/@id_concept]/@name)"/>-->
                              <xsl:choose>
                                <xsl:when test="@nickname and @nickname!=''">
                                  <xsl:value-of select="concat('/',@nickname)"/>
                                </xsl:when>
                                <xsl:otherwise>
                                  <xsl:value-of select="concat('/',$cinterm/Concepts/Concept[@id_concept=current()/@id_concept]/@name)"/>
                                </xsl:otherwise>
                              </xsl:choose>
                            </xsl:for-each>
                          </xsl:attribute>
                        </Include>
                      </xsl:for-each>
                    </Includes>
                  </Work>
                </xsl:for-each>
              </Works>
              <xsl:for-each select="CABindings/CABinding">
                <Binding p1="{@p1}" p2="{@p2}">
                  <xsl:attribute name="attr">
                    <xsl:choose>
                      <xsl:when test="$cinterm/Concepts/Concept[@id_concept=current()/../../@id_cchild]/CProperties/CProperty[@name=current()/@p2]/@attr='1' or $cinterm/Concepts/Concept[@id_concept=current()/../../@id_cchild]/CProperties/CProperty[@name=current()/@p2]/@key='1'">1</xsl:when>
                      <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                  </xsl:attribute>
                </Binding>
              </xsl:for-each>
              <xsl:for-each select="$cinterm/Concepts/Concept[@id_concept=current()/@id_cparent]/CProperties/CProperty[@key='1']">
                <KeyProperty attr="1" name="{@name}">
                  <xsl:copy-of select="CPTitles"/>
                </KeyProperty>
                <!-- + needed nonkey props -->
              </xsl:for-each>
              <xsl:for-each select="$cinterm/Concepts/Concept[@id_concept=current()/@id_cparent]/CProperties/CProperty[@onlabel='1']">
                <LabelProperty name="{@name}">
                  <xsl:attribute name="attr">
                    <xsl:choose>
                      <xsl:when test="@attr='1' or @key='1'">1</xsl:when>
                      <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                  </xsl:attribute>
                  <xsl:copy-of select="CPTitles"/>
                </LabelProperty>
              </xsl:for-each>
            </ConsistencyCheck>
          </xsl:for-each>
          <!-- + externi vybery pres reference ? -->
        </ConsistencyChecks>
     </xsl:if>
          
     <xsl:call-template name="CVITitles">
       <xsl:with-param name="c" select="$c"/>
     </xsl:call-template>
     <xsl:call-template name="CVIProperties">
       <xsl:with-param name="c" select="$c"/>
       <xsl:with-param name="rootpath"><xsl:value-of select="$rootpath"/></xsl:with-param>
       <xsl:with-param name="cvi" select="."/>
     </xsl:call-template>
     <xsl:apply-templates select="CVIRels">
       <xsl:with-param name="c" select="$c"/>
     </xsl:apply-templates>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="CVIStyles">
   <xsl:copy>
     <xsl:apply-templates select="CVIStyle">
       <xsl:sort select="@position" data-type="number" order="ascending"/>
     </xsl:apply-templates>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="CVIStyle">
   <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:copy-of select="Style"/>
   </xsl:copy>
 </xsl:template>
 
 <xsl:template match="CVIRels">
   <xsl:param name="c"/>
   <xsl:copy>
     <xsl:apply-templates select="CVIRel">
       <xsl:sort select="@position" data-type="number" order="ascending"/>
       <xsl:with-param name="c" select="$c"/>
     </xsl:apply-templates>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="CVIRel">
   <xsl:param name="c"/>
   <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:variable name="linkname"><xsl:value-of select="CVInclude/@linkname"/></xsl:variable>
     <xsl:variable name="cassoc" select="$c/CAssociations/CAssociation[@linkname=$linkname and @id_cchild=current()/CVInclude/Concept/@id_concept][1]"/>
     <xsl:attribute name="cardinality"><xsl:value-of select="$cassoc/@cardinality"/></xsl:attribute>
     <xsl:call-template name="CVIRBindings">
       <xsl:with-param name="cassoc" select="$cassoc"/>
     </xsl:call-template>
     <xsl:apply-templates select="CVInclude"/>
   </xsl:copy>
 </xsl:template>


 <xsl:template name="CVIRBindings">
   <xsl:param name="cassoc"/>
   <CVIRBindings>
     <xsl:for-each select="$cassoc/CABindings/CABinding">
       <CVIRBinding p1="{@p1}" p2="{@p2}">
         <xsl:attribute name="p1low">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="@p1"/></xsl:with-param></xsl:call-template>
         </xsl:attribute>
         <xsl:attribute name="p2low">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="@p2"/></xsl:with-param></xsl:call-template>
         </xsl:attribute>
         <xsl:attribute name="inhassoc">
           <xsl:value-of select="$cassoc/@inhassoc"/>
         </xsl:attribute>
         <xsl:attribute name="inhassocmember">
           <xsl:value-of select="$cassoc/@inhassocmember"/>
         </xsl:attribute>
         <xsl:attribute name="inhref">
           <xsl:value-of select="$cassoc/@inhref"/>
         </xsl:attribute>
       </CVIRBinding>
     </xsl:for-each>
   </CVIRBindings>
 </xsl:template>


 <xsl:template name="CVITitles">
   <xsl:param name="c"/>
   <xsl:variable name="cvititles" select="CVITitles"/>
   <CVITitles>
     <xsl:for-each select="$c/CTitles/CTitle">
       <xsl:variable name="t" select="$cvititles/CVITitle[@id_cttype=current()/@id_cttype and @id_lang=current()/@id_lang]"/>
       <xsl:choose>
         <xsl:when test="$t">
           <xsl:copy-of select="$t"/>
         </xsl:when>
         <xsl:otherwise>
           <CVITitle id_cttype="{@id_cttype}" id_lang="{@id_lang}">
             <xsl:copy-of select="Title"/>
             <xsl:copy-of select="Descr"/>
           </CVITitle>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:for-each>
     <xsl:for-each select="CVITitles/CVITitle">
       <xsl:variable name="t" select="$c/CTitles/CTitle[@id_cttype=current()/@id_cttype and @id_lang=current()/@id_lang]"/>
       <xsl:choose>
         <xsl:when test="not($t)">
           <xsl:copy-of select="."/>
         </xsl:when>
       </xsl:choose>
     </xsl:for-each>
   </CVITitles>
 </xsl:template>

 <xsl:template name="CVIProperties">
   <xsl:param name="c"/>
   <xsl:param name="rootpath"></xsl:param>
   <xsl:param name="cvi"/>
   <xsl:variable name="cvips" select="CVIProperties"/>
   <CVIProperties>
     <!-- internal properties -->
     <xsl:for-each select="$c/CProperties/CProperty">
       <xsl:sort select="@position" data-type="number" order="ascending"/>
       <xsl:variable name="t" select="$cvips/CVIProperty[@name=current()/@name]"/>
       <CVIProperty>
         <xsl:apply-templates select="@*"/>
         <xsl:if test="@key='1'">
           <xsl:attribute name="attr">1</xsl:attribute>
           <xsl:attribute name="mandat">1</xsl:attribute>
         </xsl:if>
         <xsl:if test="$t">
           <xsl:apply-templates select="$t/@*"/> <!-- will overwrite all needed -->
         </xsl:if>
         <xsl:attribute name="concept"><xsl:value-of select="../../@name"/></xsl:attribute>         
         <xsl:attribute name="conceptcoll"><xsl:value-of select="../../@coll"/></xsl:attribute>         

         <xsl:variable name="simplename"><xsl:value-of select="@name"/></xsl:variable>
         <xsl:attribute name="simplename"><xsl:value-of select="$simplename"/></xsl:attribute>
         <xsl:attribute name="simplenamelow">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$simplename"/></xsl:with-param></xsl:call-template>     
         </xsl:attribute>
         
         <xsl:attribute name="path">
           <xsl:value-of select="$c/@name"/>
         </xsl:attribute>
                        
         <xsl:if test="@ref and @ref='*'">
           <xsl:attribute name="enum">true</xsl:attribute>         
           <xsl:attribute name="ref"/>         
         </xsl:if>
                        
         <xsl:if test="@ref and @ref!='' and @ref!='*'">
           <xsl:variable name="conceptname"><xsl:value-of select="substring-before(@ref,'/')"/></xsl:variable>
           <xsl:variable name="c" select="$cinterm/Concepts/Concept[@name=$conceptname]"/>           
           <xsl:variable name="refprop"><xsl:value-of select="substring-after(@ref,'/')"/></xsl:variable>
           <xsl:attribute name="refprop"><xsl:value-of select="$refprop"/></xsl:attribute>
           <xsl:attribute name="refproplow">
             <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$refprop"/></xsl:with-param></xsl:call-template>
           </xsl:attribute>
           <xsl:attribute name="refconcept"><xsl:value-of select="$conceptname"/></xsl:attribute>
           <xsl:attribute name="refconceptcoll"><xsl:value-of select="$c/@coll"/></xsl:attribute>
           <xsl:for-each select="$c/CProperties/CProperty[@onlabel='1']">
             <xsl:sort select="@position" data-type="number" order="ascending"/>
             <RefProp code="{@name}" len="{@len}" attr="{@attr}" concept="{$conceptname}" refconcept="{$conceptname}">
               <xsl:variable name="refsimplename"><xsl:value-of select="@name"/></xsl:variable>
               <xsl:attribute name="simplename">
                 <xsl:value-of select="$refsimplename"/>
               </xsl:attribute>
               <xsl:attribute name="simplenamelow">
                 <xsl:value-of select="translate($refsimplename, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
               </xsl:attribute>
               <xsl:attribute name="lowcode">
                 <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>
               </xsl:attribute>
               <xsl:variable name="formcode"><xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$simplename"/><xsl:value-of select="$conceptname"/><xsl:value-of select="@name"/></xsl:with-param></xsl:call-template></xsl:variable>
               <xsl:attribute name="formcode">
                 <xsl:value-of select="translate($formcode,'_','x')"/>
               </xsl:attribute>
               <CVIPTitles>
                 <xsl:for-each select="CPTitles/CPTitle">
                   <CVIPTitle id_lang="{@id_lang}">
                     <xsl:copy-of select="Title"/>
                     <xsl:copy-of select="Descr"/>
                   </CVIPTitle>
                 </xsl:for-each>
               </CVIPTitles>
             </RefProp>
           </xsl:for-each>
         </xsl:if>
   
         <PropGroup>
           <xsl:attribute name="basegroup">true</xsl:attribute>
           <xsl:attribute name="reltype">none</xsl:attribute>
           <xsl:attribute name="create"><xsl:value-of select="$cvi/@create"/></xsl:attribute>
           <xsl:attribute name="detail"><xsl:value-of select="$cvi/@detail"/></xsl:attribute>
           <xsl:attribute name="update"><xsl:value-of select="$cvi/@update"/></xsl:attribute>
           <xsl:attribute name="select"><xsl:value-of select="$cvi/@select"/></xsl:attribute>
           <xsl:attribute name="delete"><xsl:value-of select="$cvi/@delete"/></xsl:attribute>
           <xsl:attribute name="virtual"><xsl:value-of select="$cvi/@virtual"/></xsl:attribute>
         </PropGroup>

         <xsl:if test="$t">
           <xsl:copy-of select="$t/DefaultFilter"/>
         </xsl:if>
   
         <xsl:choose>
           <xsl:when test="not($t)">
             <CVIPTitles>
               <xsl:for-each select="CPTitles/CPTitle">
                 <CVIPTitle id_lang="{@id_lang}">
                   <xsl:copy-of select="Title"/>
                   <xsl:copy-of select="Descr"/>
                 </CVIPTitle>
               </xsl:for-each>
             </CVIPTitles>
           </xsl:when>
           <xsl:when test="$t/@useit='1'">
             <xsl:call-template name="CVIPTitles">
               <xsl:with-param name="cp" select="."/>
               <xsl:with-param name="cvip" select="$t"/>
             </xsl:call-template>
           </xsl:when>
         </xsl:choose>
         
         <xsl:if test="CPStaticValues">
           <CPStaticValues>
             <xsl:for-each select="CPStaticValues/CPStaticValue">
               <xsl:sort select="@position"/>
               <xsl:copy-of select="."/>
             </xsl:for-each>
           </CPStaticValues>
         </xsl:if>
       </CVIProperty>
     </xsl:for-each>
    
     <!-- list external properties (will be processed and completed in the next interm (C)) -->
     <xsl:for-each select="$cvips/CVIProperty[contains(@name,'/')]">
       <xsl:sort select="@position" data-type="number" order="ascending"/>
       <CVIProperty>
         <xsl:variable name="concept_prop_tmp">
           <xsl:call-template name="substringLastAndOneBefore">
             <xsl:with-param name="str"><xsl:value-of select="@name"/></xsl:with-param>
             <xsl:with-param name="sep">/</xsl:with-param>
           </xsl:call-template>
         </xsl:variable>

         <xsl:variable name="concept_prop">
           <xsl:choose>
             <xsl:when test="substring-before($concept_prop_tmp,'/')='..'">
               <xsl:value-of select="concat(../../../../../Concept/@name,'/',substring-after($concept_prop_tmp,'/'))"/>
             </xsl:when>
             <xsl:otherwise>
               <xsl:value-of select="$concept_prop_tmp"/>
             </xsl:otherwise>
           </xsl:choose>
         </xsl:variable>

         <xsl:variable name="simplename"><xsl:value-of select="substring-after($concept_prop,'/')"/></xsl:variable>
         <xsl:variable name="simplenamelow">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$simplename"/></xsl:with-param></xsl:call-template>
         </xsl:variable>

         <xsl:variable name="conceptname"><xsl:value-of select="substring-before($concept_prop,'/')"/></xsl:variable>         
         <xsl:variable name="concept" select="$cinterm/Concepts/Concept[@name=$conceptname]"/>
         <xsl:variable name="cp" select="$concept/CProperties/CProperty[@name=$simplename]"/>

         <xsl:apply-templates select="$cp/@*"/>
         <xsl:apply-templates select="@*"/>

         <xsl:attribute name="extconcept">yes</xsl:attribute>

         <xsl:attribute name="concept">
           <xsl:value-of select="$conceptname"/>
         </xsl:attribute>
         <xsl:attribute name="conceptcoll">
           <xsl:value-of select="$concept/@coll"/>
         </xsl:attribute>

         <xsl:attribute name="simplename"><xsl:value-of select="$simplename"/></xsl:attribute>
         <xsl:attribute name="simplenamelow"><xsl:value-of select="$simplenamelow"/></xsl:attribute>


         <xsl:attribute name="path">
           <xsl:call-template name="substringBeforeLast">
             <xsl:with-param name="str"><xsl:value-of select="@name"/></xsl:with-param>
             <xsl:with-param name="sep">/</xsl:with-param>
           </xsl:call-template>
         </xsl:attribute>
         
         <xsl:variable name="nickname"><xsl:value-of select="translate(@name,'/','.')"/></xsl:variable>
         <xsl:attribute name="nickname"><xsl:value-of select="$nickname"/></xsl:attribute>
         <xsl:attribute name="nicknamelow">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$nickname"/></xsl:with-param></xsl:call-template>
         </xsl:attribute>
         
         <xsl:attribute name="lowname">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>
         </xsl:attribute>
         
         <xsl:variable name="nickname">
           <xsl:value-of select="translate(@name,'/','.')"/>
         </xsl:variable>
         <xsl:attribute name="nickname">
           <xsl:value-of select="$nickname"/>
         </xsl:attribute>
         <xsl:attribute name="nicknamelow">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$nickname"/></xsl:with-param></xsl:call-template>
         </xsl:attribute>
         
         <xsl:variable name="proppath">
           <xsl:call-template name="substringBeforeLast">
             <xsl:with-param name="str"><xsl:value-of select="@name"/></xsl:with-param>
             <xsl:with-param name="sep">/</xsl:with-param>
           </xsl:call-template>
         </xsl:variable>
         <xsl:attribute name="path">
           <xsl:value-of select="$proppath"/>
         </xsl:attribute>

         <xsl:if test="$cp/@ref and $cp/@ref='*'">
           <xsl:attribute name="enum">true</xsl:attribute>
           <xsl:attribute name="ref"/>                            
         </xsl:if>
         
         <xsl:if test="$cp/@ref and $cp/@ref!='' and $cp/@ref!='*'">
           <xsl:variable name="refconceptname"><xsl:value-of select="substring-before($cp/@ref,'/')"/></xsl:variable>
           <xsl:variable name="refc" select="$cinterm/Concepts/Concept[@name=$refconceptname]"/>           
           <xsl:variable name="refprop"><xsl:value-of select="substring-after($cp/@ref,'/')"/></xsl:variable>
           <xsl:attribute name="refprop"><xsl:value-of select="$refprop"/></xsl:attribute>
           <xsl:attribute name="refproplow">
             <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$refprop"/></xsl:with-param></xsl:call-template>
           </xsl:attribute>
           <xsl:attribute name="refconcept"><xsl:value-of select="$refconceptname"/></xsl:attribute>
           <xsl:attribute name="refconceptcoll"><xsl:value-of select="$refc/@coll"/></xsl:attribute>
           <xsl:for-each select="$refc/CProperties/CProperty[@onlabel='1']">
             <xsl:sort select="@position" data-type="number" order="ascending"/>
             <RefProp code="{@name}" len="{@len}" attr="{@attr}" concept="{$refconceptname}" refconcept="{$refconceptname}">
               <xsl:variable name="refsimplename"><xsl:value-of select="@name"/></xsl:variable>
               <xsl:attribute name="simplename">
                 <xsl:value-of select="$refsimplename"/>
               </xsl:attribute>
               <xsl:attribute name="lowcode">
                 <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>
               </xsl:attribute>
               <xsl:variable name="formcode"><xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="$simplename"/><xsl:value-of select="$refconceptname"/><xsl:value-of select="@name"/></xsl:with-param></xsl:call-template></xsl:variable>
               <xsl:attribute name="formcode">
                 <xsl:value-of select="translate($formcode,'_','x')"/>
               </xsl:attribute>
               <CVIPTitles>
                 <xsl:for-each select="CPTitles/CPTitle">
                   <CVIPTitle id_lang="{@id_lang}">
                     <xsl:copy-of select="Title"/>
                     <xsl:copy-of select="Descr"/>
                   </CVIPTitle>
                 </xsl:for-each>
               </CVIPTitles>
             </RefProp>
           </xsl:for-each>
         </xsl:if>

         <xsl:choose>
           <xsl:when test="$cp">
             <xsl:call-template name="CVIPTitles">
               <xsl:with-param name="cp" select="$cp"/>
               <xsl:with-param name="cvip" select="."/>
             </xsl:call-template>
           </xsl:when>
           <xsl:otherwise>
             <xsl:copy-of select="CVIPTitles"/>
           </xsl:otherwise>
         </xsl:choose>        

         <xsl:copy-of select="DefaultFilter"/>

       </CVIProperty>     
     </xsl:for-each>

  
     
   </CVIProperties>
 </xsl:template>



 <xsl:template name="CVIPTitles">
   <xsl:param name="cp"/>
   <xsl:param name="cvip"/>
   <CVIPTitles>
     <xsl:for-each select="$cp/CPTitles/CPTitle">
       <xsl:variable name="t" select="$cvip/CVIPTitles/CVIPTitle[@id_lang=current()/@id_lang]"/>
       <xsl:choose>
         <xsl:when test="$t">
           <xsl:copy-of select="$t"/>
         </xsl:when>
         <xsl:otherwise>
           <CVIPTitle id_lang="{@id_lang}">
             <xsl:copy-of select="Title"/>
             <xsl:copy-of select="Descr"/>
           </CVIPTitle>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:for-each>
     <xsl:for-each select="$cvip/CVIPTitles/CVIPTitle">
       <xsl:variable name="t" select="$cp/CPTitles/CPTitle[@id_lang=current()/@id_lang]"/>
       <xsl:choose>
         <xsl:when test="not($t)">
           <xsl:copy-of select="."/>
         </xsl:when>
       </xsl:choose>
     </xsl:for-each>
   </CVIPTitles>
 </xsl:template>

 

  <xsl:template name="toLowerCase">
    <xsl:param name="prm"/>
    <xsl:value-of select="translate($prm,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
  </xsl:template>  

  <xsl:template name="substringBeforeLast">
    <xsl:param name="str"></xsl:param>
    <xsl:param name="sep">/</xsl:param>
    <xsl:variable name="b"><xsl:value-of select="substring-before($str,$sep)"/></xsl:variable>
    <xsl:variable name="a"><xsl:value-of select="substring-after($str,$sep)"/></xsl:variable>
    <xsl:choose>
      <xsl:when test="not(contains($a,'/'))"><xsl:value-of select="$b"/></xsl:when>
      <xsl:otherwise><xsl:value-of select="$b"/>/<xsl:call-template name="substringBeforeLast">
          <xsl:with-param name="str"><xsl:value-of select="$a"/></xsl:with-param>
          <xsl:with-param name="sep"><xsl:value-of select="$sep"/></xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>  

  <xsl:template name="substringLastAndOneBefore">
    <xsl:param name="str"></xsl:param>
    <xsl:param name="sep">/</xsl:param>
    <xsl:variable name="b"><xsl:value-of select="substring-before($str,$sep)"/></xsl:variable>
    <xsl:variable name="a"><xsl:value-of select="substring-after($str,$sep)"/></xsl:variable>
    <xsl:choose>
      <xsl:when test="not(contains($a,'/'))"><xsl:value-of select="$str"/></xsl:when>
      <xsl:otherwise><xsl:call-template name="substringLastAndOneBefore">
          <xsl:with-param name="str"><xsl:value-of select="$a"/></xsl:with-param>
          <xsl:with-param name="sep"><xsl:value-of select="$sep"/></xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>  


  <xsl:template name="getcovroot">
    <xsl:param name="concept"/>
    <xsl:param name="hint"/>

    <xsl:choose>
      <xsl:when test="not($cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept])">
        <xsl:value-of select="$cinterm/Concepts/Concept[@id_concept=$concept]/@name"/>
      </xsl:when>
      <xsl:when test="count($cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]) &gt; 1">
        <xsl:variable name="ccviparent"><xsl:value-of select="//CVIRel[@id_cvichild=$hint]/../../@id_concept"/></xsl:variable>
        <xsl:variable name="cparent"><xsl:value-of select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept and @id_cparent=$ccviparent]/@id_cparent"/></xsl:variable>
        <xsl:choose>
          <xsl:when test="$cparent!=''">
            <xsl:call-template name="getcovroot">
              <xsl:with-param name="concept" select="$cparent"/>                                  
              <xsl:with-param name="hint" select="//CVIRel[@id_cvichild=$hint]/@id_cviparent"/>
            </xsl:call-template>                        
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="getcovroot">
              <!-- ambiguity!!! -->            
              <xsl:with-param name="concept" select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent"/>
            </xsl:call-template>
          </xsl:otherwise>                    
        </xsl:choose>          
      </xsl:when>             
      <xsl:when test="//CVIRel[@id_cvichild=$hint]/../../@id_concept=$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent">
        <xsl:call-template name="getcovroot">
          <xsl:with-param name="concept" select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent"/>          
          <xsl:with-param name="hint" select="//CVIRel[@id_cvichild=$hint]/@id_cviparent"/>
        </xsl:call-template>
      </xsl:when>        
      <xsl:otherwise>   
        <xsl:call-template name="getcovroot">
          <xsl:with-param name="concept" select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent"/>          
        </xsl:call-template>
      </xsl:otherwise> 
    </xsl:choose>
  </xsl:template>

  <xsl:template name="getcovpath">
    <xsl:param name="concept"/>
    <xsl:param name="hint"/>
    <!--<xsl:value-of select="concat('{c:',$concept,'-h:',$hint,'}')"/>-->
    
    <xsl:choose>
      <xsl:when test="not($cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept])">
        <xsl:value-of select="concat('/',$cinterm/Concepts/Concept[@id_concept=$concept]/@name)"/>
      </xsl:when>      
      <xsl:when test="count($cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]) &gt; 1">
        <xsl:variable name="ccviparent"><xsl:value-of select="//CVIRel[@id_cvichild=$hint]/../../@id_concept"/></xsl:variable>
        <xsl:variable name="cparent"><xsl:value-of select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept and @id_cparent=$ccviparent]/@id_cparent"/></xsl:variable>
        <xsl:choose>
          <xsl:when test="$cparent!=''">
            <xsl:call-template name="getcovpath">
              <xsl:with-param name="concept" select="$cparent"/>                                  
              <xsl:with-param name="hint" select="//CVIRel[@id_cvichild=$hint]/@id_cviparent"/>
            </xsl:call-template>                        
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="getcovpath">
              <!-- ambiguity!!! -->            
              <xsl:with-param name="concept" select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent"/>
            </xsl:call-template>
          </xsl:otherwise>                    
        </xsl:choose>          
        <xsl:choose>
          <!-- ambiguity!!! -->
          <xsl:when test="$cinterm/Concepts/Concept[count(CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]) &gt; 1] and $cparent!=''">
            <xsl:value-of select="concat('/',$cinterm/Concepts/Concept[@id_concept=$concept]/@name,'_',//CVInclude[@id_cvinclude=$hint]/@linkname)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat('/',$cinterm/Concepts/Concept[@id_concept=$concept]/@name)"/>      
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>             
      <xsl:when test="//CVIRel[@id_cvichild=$hint]/../../@id_concept=$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent">
        <xsl:call-template name="getcovpath">
          <xsl:with-param name="concept" select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent"/>          
          <xsl:with-param name="hint" select="//CVIRel[@id_cvichild=$hint]/@id_cviparent"/>
        </xsl:call-template>
        <xsl:value-of select="concat('/',$cinterm/Concepts/Concept[@id_concept=$concept]/@name)"/>      
      </xsl:when>        
      <xsl:otherwise>   
        <xsl:call-template name="getcovpath">
          <xsl:with-param name="concept" select="$cinterm/Concepts/Concept/CAssociations/CAssociation[@type='composite' and @id_cchild=$concept]/@id_cparent"/>          
        </xsl:call-template>
        <xsl:value-of select="concat('/',$cinterm/Concepts/Concept[@id_concept=$concept]/@name)"/>
      </xsl:otherwise>      
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
       
 
